import Stone from "./Stone";
import * as core from "../core"

const { ccclass, property } = cc._decorator;

@ccclass
export default class Player extends cc.Component {
    public static get EvtBeginContact(): string { return "发生碰撞" }
    public static get EvtPreSlove(): string { return "处理碰撞" }

    @property({ type: cc.Node })
    forceNode: cc.Node = null
    @property({ type: cc.Node })
    bodyNode: cc.Node = null

    private _rigidBody: cc.RigidBody = null
    public get rigidBody(): cc.RigidBody { return this._rigidBody }

    private _inited: boolean = false
    private _init() {
        if (this._inited) { return }
        if (!this._rigidBody._b2Body) { return }
        this._inited = true
        let mass = new b2.MassData()
        this._rigidBody._b2Body.GetMassData(mass)
        mass.mass = 1
        mass.I = 1
        mass.center.x = 0
        mass.center.y = 0
        this._rigidBody._b2Body.SetMassData(mass)
    }

    onLoad() {
        this._rigidBody = this.getComponent(cc.RigidBody)
    }

    start() {
    }

    showForce(direct: cc.Vec2, power: number) {
        if (direct) {
            this.forceNode.active = true
            this.forceNode.angle = -direct.signAngle(cc.Vec2.UP) / Math.PI * 180 + this.node.angle
            this.forceNode.scaleY = 1000 * power + 64
        } else {
            this.forceNode.active = false
        }
    }

    trigger() {
        this.forceNode.active = false

        let pos = this.node.position.clone()
        core.NodePool.getInstance().loadNode("prefab/PlayerFire", (n: cc.Node) => {
            Stone.gameEntities.node.addChild(n)
            n.setPosition(pos)
            n.getComponent(cc.ParticleSystem).resetSystem()
        }, 0)
    }

    update(dt: number) {
        this._init()
        let speed = this._rigidBody.linearVelocity.mag()
        if (speed < 200) {
            this.bodyNode.scaleX = 1
            this.bodyNode.scaleY = 1
        } else if (speed < 2000) {
            this.bodyNode.scaleX = 1 - ((speed) / 2000) * 0.2
            this.bodyNode.scaleY = 1 + ((speed) / 2000) * 0.2
        } else {
            this.bodyNode.scaleX = 1 - 0.2
            this.bodyNode.scaleY = 1 + 0.2
        }
        if (speed > 0) {
            this.bodyNode.angle = -this._rigidBody.linearVelocity.signAngle(cc.Vec2.UP) * 180 / Math.PI
        } else {
            this.bodyNode.angle = 0
        }
    }

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
        this.node.emit(Player.EvtBeginContact, contact, selfCollider, otherCollider)
    }
    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
    }
    // 每次将要处理碰撞体接触逻辑时被调用
    onPreSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
        this.node.emit(Player.EvtPreSlove, contact, selfCollider, otherCollider)
    }
    // 每次处理完碰撞体接触逻辑时被调用
    onPostSolve(contact: cc.PhysicsContact, selfCollider: cc.PhysicsCollider, otherCollider: cc.PhysicsCollider) {
    }
}
